package com.controller;

import com.domain.Video;
import com.domain.VideoView;
import com.dto.JsonResponse;
import com.dto.JsonResponseInt;
import com.dto.PageResult;
import com.service.impl.UserMomentsServiceImpl;
import com.service.impl.VideoServiceImpl;
import com.support.UserSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RequestMapping("/video")
@RestController
public class VideoApi {

    @Autowired
    private VideoServiceImpl videoServiceImpl;

    @Autowired
    private UserSupport userSupport;

    // @Autowired
    // private ElasticSearchService elasticSearchService;

    @Autowired
    private UserMomentsServiceImpl userMomentsServiceImpl;

    /**
     * 查询首页的视频
     */
    @GetMapping("/homeListVideo")
    public JsonResponseInt<Map<String, List<Video>>> homeListVideo(@RequestParam("videoPartition") String videoPartition
    ) {
        Map<String, List<Video>> result = videoServiceImpl.homeListVideo(videoPartition);
        return new JsonResponseInt<>(result);
    }

    /**
     * 给多个视频形成的列表分页
     */
    @PostMapping("/pageListVideo")
    public JsonResponseInt<PageResult<Video>> pageListVideo(@RequestBody Video video) {
        PageResult<Video> result = videoServiceImpl.pageListVideos(video);
        return new JsonResponseInt<>(result);
    }

    /**
     * 获取视频详细信息
     */
    @GetMapping("/getVideoDetail")
    public JsonResponseInt<Video> getVideoDetail(@RequestParam Long videoId) {
        Video result = videoServiceImpl.getVideoDetail(videoId, userSupport.getCurrentUserId());
        return new JsonResponseInt<>(result);
    }

    /**
     * 用户上传视频
     */
    @PostMapping("/addVideo")
    public JsonResponse<String> addVideo(@RequestBody Video video) {

//        Long userId = userSupport.getCurrentUserId();
        video.setUserId(1L);
        videoServiceImpl.addVideo(video);
        // elasticSearchService.addVideo(video);

        //up主发布视频，他的粉丝会收到动态提醒
//        UserMoments userMoments = new UserMoments();
//        userMoments.setUserId(userId);
//        userMoments.setType("0");
//        userMoments.setContentId(1L);
//        userMomentsService.addUserMoments(userMoments);
        return JsonResponse.success();
    }

    /**
     * 视频在线播放
     */
    @GetMapping("/videoSlices")
    public void viewVideoOnlineBySlices(HttpServletRequest request,
                                        HttpServletResponse response,
                                        @RequestParam String url) throws Exception {
        videoServiceImpl.viewVideoOnlineBySlices(request, response, url);

    }

    /**
     * 历史记录
     */
    @GetMapping("/getViewHistory")
    public JsonResponse<VideoServiceImpl.ScrollResult> getViewHistory(@RequestParam("lastId") Long max,
                                                                      @RequestParam(value = "offset", defaultValue = "0") Integer offset) {
        //只有登录得用户才能查看自己的历史记录
        Long userId;
        try {
            userId = userSupport.getCurrentUserId();
            VideoServiceImpl.ScrollResult result = videoServiceImpl.getViewHistory(userId, max, offset);
            return new JsonResponse<>(result);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 添加视频观看记录
     */
    @GetMapping("/addVideoView")
    public JsonResponse<String> addVideoView(@RequestBody VideoView videoView,
                                             HttpServletRequest request) {
        Long userId;
        try {
            //如果以用户身份登录，就把userId存入videoView
            userId = userSupport.getCurrentUserId();
            videoView.setUserId(userId);
            videoServiceImpl.addVideoView(videoView, request);
        } catch (Exception e) {
            videoServiceImpl.addVideoView(videoView, request);
        }
        return JsonResponse.success();
    }

    /**
     * 查看视频播放量
     */
    @GetMapping("/getVideoViewCount")
    public JsonResponse<Integer> getVideoViewCount(@RequestParam Long videoId) {
        Integer count = videoServiceImpl.getVideoViewCounts(videoId);
        return new JsonResponse<>(count);
    }

}

